#ifndef IMAGE_PLAY_ENGINE_H
#define IMAGE_PLAY_ENGINE_H

#include "SaveFile.h"
#include "ImageDisplay.h"
#include "PixelFormat.h"
#include <atomic>
#include <string>
#include <wx/slider.h>
#include <wx/window.h>
#include <wx/timer.h>
#include <mutex>
#include <functional>
#include <thread>
#include <condition_variable>

wxDECLARE_EVENT(EVT_PLAY_ENGINE, wxCommandEvent);

class ImagePlayEngine
{
public:
	ImagePlayEngine(wxWindow* parent,int wxID,SaveFile* _fileControl,ImageDisplay* _display,wxSlider* _playSlider);
	virtual ~ImagePlayEngine();
	void Reset(std::string fileName);
	bool IsPlay(); //是否播放
	void Play();
	void Pause();
	void SetPos(long pos);
	long GetPos();
	long GetAllFrame();
	std::string GetFileName();
	void SetFrameRate(unsigned frameRate);
	void SetPlayFinishHandle(std::function<void(void*)>,void* userData);
private:
	SaveFile* fileControl;
	ImageDisplay* display;
	wxSlider* playSlider;
	wxWindow* parentHandle;
	int wxID;

	std::thread FileReadThread;
	void FileReadFun();
	std::atomic<bool> ThreadExitFlag;
	std::atomic<bool> FileReadEOF=false;
	std::condition_variable FromPauseWake; //暂停后线程休眠，再次播放的时候就唤醒
	std::atomic<bool> PlayStatus;  //播放暂停
	std::mutex PlayLCK; //线程运行就会获得这把锁，暂停就会释放，可以用于判断线程是否在执行

	std::atomic<int> playPos;
	std::function<void(void*)> PlayFinishHandle;
	void* PlayFinishUserData;

	int32_t width;
	int32_t height;
	int32_t frameCount;
  pixelFormat ImageFormat;

	std::atomic<unsigned> frameRate;

	void on_Timeout(wxTimerEvent& event);
};

#endif // !IMAGE_PLAY_ENGINE_H

